/*
 *  Copyright (C) 2010 JS.MinecraftSavior
 *
 *  This file is part of the Minecraft Savior utility.
 *
 *  Minecraft Savior is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Minecraft Savior is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.utility.MinecraftSavior.ui;

import java.awt.Desktop;
import java.awt.Point;
import java.awt.Rectangle;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import javax.swing.event.HyperlinkEvent;

public class Documentation extends javax.swing.JDialog {
    private static final String DOC_FILE_NAME = "MCSHelp.html";

    // Constructor
    public Documentation(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        insertDocumentationHTML();
        centerInParent(parent);
    }

    // Set the editor pane to DOC_FILE_NAME
    private void insertDocumentationHTML() {
        URL url = Documentation.class.getClassLoader().getResource(DOC_FILE_NAME);
        try {
            docEditorPane.setPage(url);
        } catch (IOException ex) {
        }
    }

    // Center this dialog in the parent frame
    private void centerInParent(java.awt.Frame parent) {
        Rectangle parBounds = parent.getBounds();
        int x = Math.max(0, parBounds.x + (parBounds.width - getSize().width) / 2);
        int y = Math.max(0, parBounds.y + (parBounds.height - getSize().height) / 2);
        setLocation(new Point(x, y));
    }

    @SuppressWarnings("unchecked")
    // Initialize all components
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        docLabel = new javax.swing.JLabel();
        docScrollPane = new javax.swing.JScrollPane();
        docEditorPane = new javax.swing.JEditorPane();
        closeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MCS Documentation");

        docLabel.setFont(new java.awt.Font("Tahoma", 0, 14));
        docLabel.setText("MCS Help & Documentation");

        docEditorPane.setContentType("text/html");
        docEditorPane.setEditable(false);
        docEditorPane.addHyperlinkListener(new javax.swing.event.HyperlinkListener() {
            public void hyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {
                docEditorPaneHyperlinkUpdate(evt);
            }
        });
        docScrollPane.setViewportView(docEditorPane);

        closeButton.setText("Return to MCS");
        closeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(docLabel)
                    .addComponent(docScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 547, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(closeButton))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(docLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(docScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 233, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(closeButton)
                .addContainerGap(17, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Open default browser when a hyperlink is clicked
    private void docEditorPaneHyperlinkUpdate(javax.swing.event.HyperlinkEvent evt) {//GEN-FIRST:event_docEditorPaneHyperlinkUpdate
        if (evt.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
            try {
                Desktop.getDesktop().browse(evt.getURL().toURI());
            } catch (URISyntaxException ex) {
            } catch (IOException ex) {
            }
        }
    }//GEN-LAST:event_docEditorPaneHyperlinkUpdate

    // Dispose dialog
    private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton closeButton;
    private javax.swing.JEditorPane docEditorPane;
    private javax.swing.JLabel docLabel;
    private javax.swing.JScrollPane docScrollPane;
    // End of variables declaration//GEN-END:variables

}
